package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.breadtrip.R;

/**
 * Created by sunsg on 16-5-4.
 * 如何得到圆上每个点的坐标？

 解决思路：根据三角形的正玄、余弦来得值；

 假设一个圆的圆心坐标是(a,b)，半径为r，

 则圆上每个点的X坐标=a + Math.cos(2*Math.PI / 360) * r ；Y坐标=b + Math.sin(2*Math.PI / 360) * r ；


 */
public class TestWatchView extends View{
    private float mBorderWith;
    private int mBorderColor;
    private Paint mPaint;
    private RectF mRectF;
    private int mRadius;
    private float mWidth;
    private float mHeight;
    private int mSmallLength;
    private int mLargerLength;
    private float mStartX;
    private float mStartY;
    private float mEndX;
    private float mEndY;

    /**
     * 移动速度
     */
    private int mDegree = -90;

    public TestWatchView(Context context) {
        this(context,null);
    }

    public TestWatchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TestWatchView);
        try{
            mBorderColor = array.getColor(R.styleable.TestWatchView_test_watch_color,0xffff0000);
            mBorderWith = array.getDimension(R.styleable.TestWatchView_test_watch_width,2);
        }finally {
            array.recycle();
        }
        init(context);
    }

    public TestWatchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context){
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBorderWith);
        mPaint.setAntiAlias(true);
        mPaint.setColor(mBorderColor);
        mPaint.setTextSize(30);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mRectF = new RectF(left,top,right,bottom);
        mWidth = mRectF.right - mRectF.left;
        mHeight = mRectF.bottom - mRectF.top;
        mSmallLength = 10;
        mLargerLength = 20;
        mRadius = (int) (Math.min(mWidth,mHeight)/4);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mRectF.centerX(),mRectF.centerY(),mRadius,mPaint);
        canvas.drawCircle(mRectF.centerX(),mRectF.centerY(),20,mPaint);
        canvas.save();
        mStartX = mRectF.centerX() + mRadius;
        mStartY = mRectF.centerY();
        mEndY = mRectF.centerY();
        for (int i = 0;i<60;i++){

            if(i%5 == 0){
//                mStartX = mRectF.centerX() + (float) Math.cos(Math.PI/180 * i * 6) * mRadius;//圆点上的坐标点 X
//                mStartY = mRectF.centerY() + (float) Math.sin(Math.PI/180 * i * 6) * mRadius;//圆点上的坐标点 Y
//                mEndX = mStartX + (float) Math.cos(Math.PI/180 * i * 6) * mLargerLength;
//                mEndY = mStartY + (float) Math.sin(Math.PI/180 * i * 6) * mLargerLength;

                mEndX = mStartX + mLargerLength;
                float h = mPaint.measureText("7");
                canvas.drawText(i/5+"",mEndX+10,mEndY + h/2,mPaint);
            }else{
                mEndX = mStartX + mSmallLength;
            }

            canvas.drawLine(mStartX,mStartY,mEndX,mEndY,mPaint);

            canvas.rotate(6,mRectF.centerX(),mRectF.centerY()); //旋转画纸
        }
        canvas.restore();

//        canvas.save();
//        canvas.drawLine(mRectF.centerX() + mRadius,mRectF.centerY(),mRectF.centerX() + mRadius +mLargerLength,mRectF.centerY(),mPaint);
//        canvas.rotate(6,mRectF.centerX(),mRectF.centerY()); //旋转画纸
//        canvas.drawLine(mRectF.centerX() + mRadius,mRectF.centerY(),mRectF.centerX() + mRadius +mSmallLength,mRectF.centerY(),mPaint);
//        canvas.restore();

        //画长线

        mStartX = mRectF.centerX();
        mStartY = mRectF.centerY();
        mEndX = mStartX + (float) Math.cos(Math.PI / 180 * mDegree) * mRadius;
        mEndY = mStartY + (float) Math.sin(Math.PI / 180 * mDegree) * mRadius;

        canvas.drawLine(mStartX,mStartY,mEndX,mEndY,mPaint);
    }

    public void setDegree(int degree){
        mDegree = degree;
        postInvalidate();
    }

}
